package com.bw.day03.order

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.net.mvvm.BaseActivity
import com.bw.base.net.param.ParamCreateOrder
import com.bw.day03.R
import com.bw.day03.adapter.PreOrderAdapter
import com.bw.day03.databinding.ActivityCreatOrderBinding

@Route(path = "/app/CreateOrderActivity")
class CreateOrderActivity : BaseActivity<ActivityCreatOrderBinding, OrderViewModel>() {
    lateinit var preOrderAdapter: PreOrderAdapter
    @Autowired
    @JvmField
    var preOrderNo=""
    var addressId =0
    override fun initView() {
        ARouter.getInstance().inject(this)
        preOrderAdapter= PreOrderAdapter()
        binding.rvPreorder.adapter=preOrderAdapter
        binding.rvPreorder.layoutManager= LinearLayoutManager(this)
        viewModel.getPreOrder(preOrderNo)
        binding.tvCreateOrder.setOnClickListener {
            val paramCreateOrder= ParamCreateOrder()
            paramCreateOrder.preOrderNo=preOrderNo
            paramCreateOrder.addressId=addressId
            viewModel.createOrder(paramCreateOrder)
        }
    }
    override fun initData() {
        viewModel.getSuccess.observe(this){
            if (it.code==200){
                preOrderAdapter.data.clear()
                preOrderAdapter.data.addAll(it.data.orderInfoVo.orderDetailList)
                preOrderAdapter.notifyDataSetChanged()
                binding.tvUser.text = "${it.data.orderInfoVo.realName}  ${it.data.orderInfoVo.phone}"
                //地址
                binding.tvAddress.text = "${it.data.orderInfoVo.province}${it.data.orderInfoVo.city}${it.data.orderInfoVo.district}${it.data.orderInfoVo.detail}"
                binding.tvCount.text = "共${it.data.orderInfoVo.orderProNum}种商品"
                binding.tvTotal.text = "合计：${it.data.orderInfoVo.payFee}"
                addressId = it.data.orderInfoVo.addressId//给配送地址id赋值
            }
        }
        viewModel.createSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //生成一个未支付的订单
                ARouter.getInstance().build("/app/PayActivity").withString("orderNo",it.data.orderNo).navigation()
            }
        }
    }
    override fun getViewModel(): Class<OrderViewModel>  = OrderViewModel::class.java

    override fun getLayoutId(): Int =  R.layout.activity_creat_order

}